﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.UserControl;
using BestBuy.Common;
using BestBuy.DAL;
namespace BestBuy.PageUser
{
    public partial class AddNewProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["IDDN"].ToString().Equals(""))
            {
                Response.Redirect("~/Account/Login.aspx");
            }
        }
        protected void PostNewListingWizard_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (PostNewListingWizard.ActiveStep == SelectCategoryStep)
            {
                Page.Validate("SelectCategoryValidators");
            }
            if (PostNewListingWizard.ActiveStep == ProductDetailsStep)
            {
                Page.Validate("ProductDetailValidations");
            }

            if (!IsValid)
            {
                e.Cancel = true;
                return;
            }
        }
        protected void PostNewListingWizard_ActiveStepChanged(object sender, EventArgs e)
        {
            if (PostNewListingWizard.ActiveStep == SelectCategoryStep)
            {
                var nextButton = PostNewListingWizard.FindControl("StepNextButton") as Button;
                if (nextButton != null)
                {
                    nextButton.ValidationGroup = "SelectCategoryValidators";
                }
            }
            if (PostNewListingWizard.ActiveStep == ProductDetailsStep)
            {
                var nextButton = PostNewListingWizard.FindControl("StepNextButton") as Button;
                if (nextButton != null)
                {
                    nextButton.ValidationGroup = "ProductDetailValidations";
                }
                int? productID = SelectCategoryControl.SelectedSubCategoryID;
                if (productID != null)
                {
                    ProductDetailControl.LoadCategory((int)productID);
                }
            }
            if (PostNewListingWizard.ActiveStep == PreviewStep)
            {
                LoadPreviewProduct(ProductDetailControl, PreviewProductInfoControl);
            }
        }
        private void LoadPreviewProduct(ProductInfoDetail detail, ProductInfoPreview preview)
        {

            preview.setCategoryID = detail.getIdCategory;
            preview.setProductDetail = detail.getProductDetail;
            preview.setProductName = detail.getDescription;
            preview.setModel = detail.getModel;
            preview.setKeyWord = detail.getKeyWord;
            preview.setDescription = detail.getDescription;
            preview.setUrlPhoto = detail.getUrlPhoto;
            preview.setUrlBrochoure = detail.getUrlBrochoure;
            preview.setSupplyingAbility = detail.getSupplyingAbility;
            preview.setDeliverTime = detail.getDeliverTime;
            preview.setPlaceOfOrigin = detail.getPlaceOfOrigin;
            preview.setMinimumOrder = detail.getMinimumOrder;
            preview.setPayment = detail.getPayment;
            detail.setPhoto();
            detail.setBrochoure();
        }
        protected void SideBar_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            LinkButton button = e.Item.FindControl("sideBarButton") as LinkButton;
            if (button != null)
            {
                if (e.Item.DisplayIndex <= PostNewListingWizard.ActiveStepIndex)
                {
                    button.Enabled = true;
                    button.CssClass = "enabled";
                }
                else
                {
                    button.Enabled = false;
                    button.CssClass = "disabled";
                }

            }
        }
        private void CreateAution(ProductInfoDetail productdetail)
        {
            SanPham sp = new SanPham();            
            sp.IDDanhMuc = productdetail.getIdCategory;
            sp.ChiTietSanPham = productdetail.getProductDetail;
            sp.TenSanPham = productdetail.getProductName;
            sp.MaLoaiSanPham = productdetail.getModel;
            sp.TuKhoa = productdetail.getKeyWord;
            sp.MoTa = productdetail.getDescription;
            sp.TaiLieu = productdetail.getTaiLieu();
            sp.HinhAnh = productdetail.getHinhAnh();
            sp.KyHanGiaoHang = productdetail.getDeliverTime;
            sp.KhaNangCungCap = productdetail.getSupplyingAbility;
            sp.NoiXuatXu = productdetail.getPlaceOfOrigin;
            sp.SoLuongDatToiThieu = productdetail.getMinimumOrder;
            int[] paymentID = productdetail.getPayment;
            string strPayment = "";
            for (int i = 0; i < paymentID.Count(); i++)
            {
                strPayment += paymentID[i].ToString() + "+";
            }
            sp.PhuongThucThanhToan = strPayment;
            sp.IDTaiKhoan = int.Parse(Session["IDDN"].ToString());
            sp.TinhTrang = (int)SanPhamStatus.Unverified;
            sp.NgayDang = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + DateTime.Now.Year;
            cDangSanPham.AddProduct(sp);
        }
        protected void PostNewListingWizard_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (!IsValid)
            {
                e.Cancel = true;
                return;
            } 
            try
            {
                CreateAution(ProductDetailControl);
                ProductDetailControl.Savephoto();
                ProductDetailControl.SaveBrochoure();
                Response.Redirect("~/PageUser/MbSellling.aspx");
            }
            catch (System.Exception ex)
            {
                
            }
           
        }
    }
}